package com.qqpz.sign_project.controller;


import com.google.gson.Gson;
import com.qqpz.sign_project.entity.Records;
import com.qqpz.sign_project.entity.User;
import com.qqpz.sign_project.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/v1/user")
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping(path = "/{code}/{name}")
    public String wxLogin(@PathVariable("code")String code,@PathVariable("name") String name){
        System.out.println("11111");
        Map<String,Object> map=new HashMap<>();
        try {
            User user = userService.wxLogin(code, name);
            map.put("user",user);
            map.put("result","success");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result","fail");
        }
        return new Gson().toJson(map);

    }

    @GetMapping(path = "/")
    public String queryAllUser(){
        List<User> list = userService.queryAllUser();
        Map<String,Object> map=new HashMap<>();
        if(list!=null){
            map.put("result","success");
            map.put("users",list);
            return new Gson().toJson(map);
        }else{
            map.put("result","fail");
            return new Gson().toJson(map);
        }
    }
    @GetMapping(path = "/{id}")
    public String queryById(@PathVariable("id") String id){
        User user = userService.queryUserById(id);
        Map<String,Object> map=new HashMap<>();
        if(user!=null){
            map.put("result","success");
            map.put("user",user);
            return new Gson().toJson(map);
        }else{
            map.put("result","fail");
            return new Gson().toJson(map);
        }
    }
    @GetMapping(path = "/name/{name}")
    public String queryByName(@PathVariable("name") String name){
        List<User> list = userService.queryUserByName(name);
        Map<String,Object> map=new HashMap<>();
        if(list!=null){
            map.put("result","success");
            map.put("users",list);
            return new Gson().toJson(map);
        }else{
            map.put("result","fail");
            return new Gson().toJson(map);
        }
    }
    @PostMapping(path = "/update")
    public String updateUser(@RequestBody User user){
        int i= 0;
        try {
            i = userService.updateUserById(user);
        } catch (Exception e) {
            e.printStackTrace();
            i=0;
        }
        Map<String,Object> map=new HashMap<>();
        if(i!=0){
            map.put("result","success");
            return new Gson().toJson(map);
        }else{
            map.put("result","fail");
            return new Gson().toJson(map);
        }
    }
    //按id删除
    @PostMapping(path ="/delete")
    public String deleteUserById(@RequestBody User user){
        int i = 0;
        try {
            i = userService.deleteUserById(user.getId());
        } catch (Exception e) {
            e.printStackTrace();
            i=0;
        }
        Map<String, String> map = new HashMap<>();
        if(i!=0){
            map.put("result","success");
            return new Gson().toJson(map);
        }else{
            map.put("result"," fail");
            return new Gson().toJson(map);
        }
    }
}
